package co.fitstart.admin.web.controller.finance;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.ProductService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.product.Product;

@Controller
@RequestMapping("/inventory-files")
@MenuSetting("menu-finance")
public class InventoryFileController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-inventory-file")
    public ModelAndView list(@RequestParam(name = "export", required = false, defaultValue = "false") boolean export,
                             Page page,
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("finance/inventory/inventory-file-list");
        
        List<Product> products = productService.list(export, page);
        modelAndView.addObject("products", products);
        
        modelAndView.addObject("page", page);
        modelAndView.addObject("export", export);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("finance/inventory/inventory-file-export");
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("inventory-file.xls"));
        
        // list all unexport student
        List<Product> products = productService.listUnexportProduct();
        modelAndView.addObject("products", products);
        
        // update student's exportArchives
        if(!products.isEmpty()) {
            List<Long> productIds = CollectionUtils.extractPropertyList(products, "id");
            productService.exportInventory(productIds, login);
        }
        
        return modelAndView;
    }
    

}
